package top.hundreds.oasys.controller.FrontController;

import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Matter;
import top.hundreds.oasys.bean.vo.MatterVo;
import top.hundreds.oasys.service.MatterService;
import top.hundreds.oasys.utils.MvcHttpUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Matter)表控制层
 *
 * @author lcf
 * @since 2021-01-29 19:15:53
 */
@RestController
@RequestMapping("/front/matter")
public class MatterController {
    /**
     * 服务对象
     */
    @Resource
    private MatterService matterService;

    /**
     * 获取单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/get/{id}")
    public ApiResult<MatterVo> selectOne(@PathVariable Long id) {
        MatterVo matterVo = this.matterService.buildVo(this.matterService.get(id));
        return ApiResult.success(matterVo);
    }

    @PostMapping("/list")
    public ApiResult<List<MatterVo>> list(@RequestBody PageParam<Matter> pageParam) {
        if(pageParam.getAndQuery() == null) {
            Matter andQuery = new Matter();
            andQuery.setUserId(MvcHttpUtil.getCurrUserId());
            pageParam.setAndQuery(andQuery);
        }else {
            Matter andQuery = pageParam.getAndQuery();
            andQuery.setUserId(MvcHttpUtil.getCurrUserId());
            pageParam.setAndQuery(andQuery);
        }
        List<MatterVo> list = this.matterService.list(pageParam);
        return ApiResult.success(list);
    }

    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody Matter matter) {
        if (matter.getId() == null) {
            matter.setUserId(MvcHttpUtil.getCurrUserId());
            // 新增操作
            this.matterService.insert(matter);
        } else {
            // 更新操作
            this.matterService.update(matter);
        }
        return ApiResult.success();
    }

    /**
     * 删除事项
     *
     * @param id 事项id
     * @return success
     */
    @DeleteMapping("/remove/{id}")
    public ApiResult<String> remove(@PathVariable Long id) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        // 删除操作
        this.matterService.delete(id);
        return ApiResult.success();
    }

    /**
     * 完成/未完成事项
     *
     * @param id 事项id
     * @param status 事项状态
     * @return success
     */
    @GetMapping("/changeStatus/{id}/{status}")
    public ApiResult<String> complete(@PathVariable Long id, @PathVariable Integer status) {
        if (id == null || status == null) {
            return ApiResult.fail("请传入参数");
        }
        this.matterService.setStatus(id, status);
        return ApiResult.success();
    }

}
